package com.epsilon.commons.util;

import java.util.List;

import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.swt.widgets.Combo;

import com.epsilon.commons.util.map.model.IMapeable;
import com.epsilon.commons.util.map.viewers.IMapeableComboConfig;

public class CombosUtil {

	private CombosUtil() {
	}

	public static ComboViewer configurarIMapeableCombo( Combo combo, boolean mostrarCodigo ) {
		ComboViewer viewer = new ComboViewer( combo );
		IMapeableComboConfig config = new IMapeableComboConfig();
		config.setMostrarCodigo( mostrarCodigo );
		viewer.setLabelProvider( config );
		viewer.setContentProvider( config );

		return viewer;
	}

	public static void resetIMapeableCombo( ComboViewer viewer ) {
		Object lista = viewer.getInput();
		viewer.setInput( null );
		viewer.getCombo().select( -1 );
		viewer.setInput( lista );
	}

	public static IMapeable getElementoSeleccionado( ComboViewer viewer ) {
		IMapeable mapeable = null;
		Combo combo = viewer.getCombo();

		if( combo.getSelectionIndex() > -1 ) {
			mapeable = (IMapeable)viewer.getElementAt( combo.getSelectionIndex() );
		}

		return mapeable;
	}

	@SuppressWarnings( "unchecked" )
	public static void setComboSelection( ComboViewer comboViewer, IMapeable seleccion ) {
		List< IMapeable > comboList = (List< IMapeable >)comboViewer.getInput();
		for( int i = 0; i < comboList.size(); i++ ) {
			if( ((IMapeable)comboViewer.getElementAt( i )).getCodigo().trim().equals( seleccion.getCodigo().trim() ) ) {
				comboViewer.getCombo().select( i );
				break;
			}
		}
	}
}